﻿using System;
using System.Collections.Generic;
using System.Data;
using PPI.Client.Logic.Distribution;
using PPI.Entities.Distribution;

namespace PPI.Controls.Grid
{
    public partial class GridDistributionSectionalizers : GridBase
    {
        private List<ESectionalizer> m_Sectionalizers;

        public List<ESectionalizer> CurrentSectionalizers
        {
            get
            {
                return m_Sectionalizers;
            }
        }

        public EDistributionPole Pole { get; set; }

        public GridDistributionSectionalizers()
        {
            InitializeComponent();
            cmiEditWhithOutImages.Visible = false;
        }

        public void LoadSectionalizers(string connectionString)
        {
            m_Sectionalizers = LSectionalizer.GetPoleSectionalizers(Pole, connectionString);
            LoadSectionalizers(m_Sectionalizers);
        }

        public void LoadSectionalizers(List<ESectionalizer> sectionalizers)
        {
            m_Sectionalizers = sectionalizers;
            RefreshGrid();
        }

        public void RefreshGrid()
        {
            var dt = new DataTable("Sectionalizer");

            dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Latitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Longitude", typeof(double)));
            dt.Columns.Add(new DataColumn("Serial Number", typeof(string)));

            foreach (ESectionalizer sectionalizer in m_Sectionalizers)
                dt.Rows.Add(sectionalizer.Id, sectionalizer.Latitude == double.MinValue ? 0 : sectionalizer.Latitude, sectionalizer.Longitude == double.MinValue ? 0 : sectionalizer.Longitude, sectionalizer.SerialNumber);

            datagridBase.AutoGenerateColumns = true;
            datagridBase.DataSource = dt;
            datagridBase.Refresh();
        }

    }
}
